﻿namespace Nello.Web.Controls
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class NPager : WebControl
    {
        private int index = 0;
        private int size = 10;
        private int total = 0;
        private string txt = " 共{0}页 第{1}页 共{2}条记录 当前{3}条记录";

        public event EventHandler PageIndexChanged;

        public NPager()
        {
            this.InitButtons();
        }

        private void InitButtons()
        {
            Button lbFirst = new Button();
            Button lbPrevious = new Button();
            Button lbNext = new Button();
            Button lbLast = new Button();
            lbFirst.Text = " |< ";
            lbPrevious.Text = " < ";
            lbNext.Text = " > ";
            lbLast.Text = " >| ";
            lbFirst.Click += new EventHandler(this.lbFirst_Click);
            lbPrevious.Click += new EventHandler(this.lbPrevious_Click);
            lbNext.Click += new EventHandler(this.lbNext_Click);
            lbLast.Click += new EventHandler(this.lbLast_Click);
            this.Controls.Add(lbFirst);
            this.Controls.Add(lbPrevious);
            this.Controls.Add(lbNext);
            this.Controls.Add(lbLast);
        }

        private void lbFirst_Click(object sender, EventArgs e)
        {
            this.PageIndex = 0;
            if (this.PageIndexChanged != null)
            {
                this.PageIndexChanged(sender, e);
            }
        }

        private void lbLast_Click(object sender, EventArgs e)
        {
            this.PageIndex = this.PageCount - 1;
            if (this.PageIndexChanged != null)
            {
                this.PageIndexChanged(sender, e);
            }
        }

        private void lbNext_Click(object sender, EventArgs e)
        {
            if (this.PageIndex < (this.PageCount - 1))
            {
                this.PageIndex++;
            }
            if (this.PageIndexChanged != null)
            {
                this.PageIndexChanged(sender, e);
            }
        }

        private void lbPrevious_Click(object sender, EventArgs e)
        {
            if (this.PageIndex > 0)
            {
                this.PageIndex--;
            }
            if (this.PageIndexChanged != null)
            {
                this.PageIndexChanged(sender, e);
            }
        }

        protected override void LoadControlState(object savedState)
        {
            Queue qu = (Queue) savedState;
            base.LoadControlState(qu.Dequeue());
            this.total = int.Parse(qu.Dequeue().ToString());
            this.index = int.Parse(qu.Dequeue().ToString());
            this.size = int.Parse(qu.Dequeue().ToString());
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Page.RegisterRequiresControlState(this);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writer.Write(this.txt, new object[] { this.PageCount, this.PageNumber, this.TotalRecords, this.CurrentRecords });
        }

        protected override object SaveControlState()
        {
            Queue qu = new Queue();
            qu.Enqueue(base.SaveControlState());
            qu.Enqueue(this.total);
            qu.Enqueue(this.index);
            qu.Enqueue(this.size);
            return qu;
        }

        public int CurrentRecords
        {
            get
            {
                if ((this.PageIndex == (this.PageCount - 1)) && ((this.TotalRecords % this.PageSize) != 0))
                {
                    return (this.TotalRecords % this.PageSize);
                }
                return this.PageSize;
            }
        }

        public int PageCount
        {
            get
            {
                return (int) Math.Ceiling((double) (((double) this.TotalRecords) / ((double) this.PageSize)));
            }
        }

        [Browsable(false)]
        public int PageIndex
        {
            get
            {
                return this.index;
            }
            set
            {
                this.index = value;
            }
        }

        public int PageNumber
        {
            get
            {
                return (this.PageIndex + 1);
            }
        }

        public int PageSize
        {
            get
            {
                return this.size;
            }
            set
            {
                this.size = value;
            }
        }

        public string Text
        {
            get
            {
                return this.txt;
            }
            set
            {
                this.txt = value;
            }
        }

        [Browsable(false)]
        public int TotalRecords
        {
            get
            {
                return this.total;
            }
            set
            {
                this.total = value;
                this.index = 0;
            }
        }
    }
}

